package stark.app.function.signtool;

import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.stark.sign.tool.SignDataProvider;
import com.stark.sign.tool.api.SignApi;
import com.stark.sign.tool.bean.ColorBean;
import com.stark.sign.tool.bean.StyleBean;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.app.R;
import stark.app.databinding.ActivitySignToolDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.retrofit.IReqRetCallback;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.RxUtil;

/**
 * Author：daizhongbin on 2021/9/23
 * Email：1027154338@qq.com
 * Desc:
 */
public class SignToolDemoActivity extends BaseNoModelActivity<ActivitySignToolDemoBinding> {

    private SignColorAdapter mColorAdapter;
    private SignStyleAdapter mStyleAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_sign_tool_demo;
    }

    @Override
    protected void initView() {
        //rv color
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mDataBinding.rvColor.setLayoutManager(layoutManager);
        SignColorAdapter colorAdapter = new SignColorAdapter();
        mColorAdapter = colorAdapter;
        colorAdapter.setNewInstance(SignDataProvider.getColors(this));
        colorAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                colorAdapter.setSelPos(position);
            }
        });
        mDataBinding.rvColor.setAdapter(colorAdapter);

        //rv style
        layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mDataBinding.rvStyle.setLayoutManager(layoutManager);
        SignStyleAdapter styleAdapter = new SignStyleAdapter();
        mStyleAdapter = styleAdapter;
        styleAdapter.setNewInstance(SignDataProvider.getStyles(this));
        styleAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                styleAdapter.setSelPos(position);
            }
        });
        mDataBinding.rvStyle.setAdapter(styleAdapter);

        mDataBinding.btnSign.setOnClickListener(v -> {
            sign();
        });
        mDataBinding.btnSave.setOnClickListener(v -> {
            save();
        });

    }

    @Override
    protected void initData() {
    }

    private void sign() {
        String name = mDataBinding.etName.getText().toString();
        if (name.length() < 2 || name.length() > 6) {
            ToastUtils.showShort("请输入2-6个字符");
            mDataBinding.etName.requestFocus();
            return;
        }
        //color
        ColorBean colorBean = mColorAdapter.getSelColor();
        if (colorBean == null) {
            ToastUtils.showShort("请选择要签名的颜色");
            return;
        }
        //style
        StyleBean styleBean = mStyleAdapter.getSelStyle();
        if (styleBean == null) {
            ToastUtils.showShort("请选择要签名的字体样式");
            return;
        }
        showDialog("签名中...");
        SignApi.getSignNameImgUrl(name, styleBean, colorBean, new IReqRetCallback<Bitmap>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable Bitmap result) {
                hideDialog();
                if (success) {
                    mDataBinding.ivRet.setImageBitmap(result);
                    mDataBinding.btnSave.setVisibility(View.VISIBLE);
                } else {
                    ToastUtils.showShort(msg);
                }
            }
        });
    }

    private void save() {
        showDialog("保存中...");
        RxUtil.create(new RxUtil.Callback<Uri>() {
            @Override
            public void doBackground(ObservableEmitter<Uri> emitter) {
                Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.ivRet);
                Uri uri = FileP2pUtil.saveBitmap2Png(SignToolDemoActivity.this, bitmap);
                emitter.onNext(uri);
            }

            @Override
            public void accept(Uri uri) {
                hideDialog();
                if (uri != null) {
                    ToastUtils.showShort("保存成功，请到系统相册中查看");
                } else {
                    ToastUtils.showShort("保存失败");
                }
            }
        });
    }
}
